package com.linran.structure_algorithm.算法.常用算法.a7_kruskal;

import java.util.Arrays;
import java.util.Objects;

/**
 * 克鲁斯卡尔算法
 * <p>
 * 最小生成树问题
 */
public class KruskalAlgorithm {
    private int edgeNum = 0;
    private char[] vertex;
    private int[][] matrix;
    public static final int INF = Integer.MAX_VALUE;

    public static void main(String[] args) {
        char[] vertex = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
        //克鲁斯卡尔算法的临街举证
        int[][] matrix = {
                /*A*//*B*//*C*//*D*//*E*//*F*//*G*/
                /*A*/ {0, 12, INF, INF, INF, 16, 14},
                /*B*/ {12, 0, 10, INF, INF, 7, INF},
                /*C*/ {INF, 10, 0, 3, 5, 6, INF},
                /*D*/ {INF, INF, 3, 0, 4, INF, INF},
                /*E*/ {INF, INF, 5, 4, 0, 2, 8},
                /*F*/ {16, 7, 6, INF, 2, 0, 9},
                /*G*/ {14, INF, INF, INF, 8, 9, 0}
        };
        KruskalAlgorithm kruskal = new KruskalAlgorithm(vertex, matrix);
        kruskal.printf();
//        kruskal.getEdges();
        kruskal.kruskal();
    }

    public KruskalAlgorithm(char[] vertex, int[][] matrix) {
        this.vertex = Arrays.copyOf(vertex, vertex.length);
        this.matrix = new int[vertex.length][vertex.length];
        for (int i = 0; i < vertex.length; i++) {
            System.arraycopy(matrix[i], 0, this.matrix[i], 0, vertex.length);
        }

        for (int i = 0; i < vertex.length; i++) {
            for (int j = i + 1; j < vertex.length; j++) {
                if (this.matrix[i][j] != INF) {
                    edgeNum++;
                }
            }
        }
    }

    public void printf() {
        for (int[] ints : this.matrix) {
            for (int anInt : ints) {
                System.out.printf("%12d", anInt);
            }
            System.out.println();
        }
    }

    public void sortEdges(EData[] eData) {
        if (Objects.isNull(eData)) {
            return;
        }
        EData temp;
        for (int i = 0; i < eData.length - 1; i++) {
            for (int j = 0; j < eData.length - 1 - i; j++) {
                if (eData[j].weight > eData[j + 1].weight) {
                    temp = eData[j];
                    eData[j] = eData[j + 1];
                    eData[j + 1] = temp;
                }
            }
        }
    }

    public int position(char ch) {
        for (int i = 0; i < vertex.length; i++) {
            if (vertex[i] == ch) {
                return i;
            }
        }
        return -1;
    }

    public EData[] getEdges() {
        int index = 0;
        EData[] eData = new EData[edgeNum];
        for (int i = 0; i < vertex.length; i++) {
            for (int j = i + 1; j < vertex.length; j++) {
                if (matrix[i][j] != INF) {
                    eData[index++] = new EData(vertex[i], vertex[j], matrix[i][j]);
                }
            }
        }
        return eData;
    }

    public void kruskal() {
        int index = 0; //表示最后结果数组的索引
        int[] ends = new int[edgeNum]; // 用于保存"已有最小生成树"中每个顶点再最小生成树中的终点
        //最后的最小生成树
        EData[] rets = new EData[edgeNum];
        //一共12条边
        EData[] edges = getEdges();
        //按照边的权值大小排序
        sortEdges(edges);
        //遍历edges数组，将边添加到最小生成树中时，判断是准备加入的边是否形成了回路，如果没有，旧加入rets，否则不能加入
        for (int i = 0; i < edgeNum; i++) {
            //获取到第i条边的第一个顶点
            int p1 = position(edges[i].start);
            //获取到第i天边的第二个顶点
            int p2 = position(edges[i].end);
            //获取p1这个顶点在已有的最小生成树中的终点是哪个
            int m = getEnd(ends, p1);
            int n = getEnd(ends, p2);
            //是否构成会回路
            if (m != n) { //不构成回路
                ends[m] = n; // 设置m在已有最小生成树中的终点 <E,F>
                rets[index++] = edges[i]; //有一条边加入到rest数组
            }
        }

        System.out.println("最小生成树=");
        for (int i = 0; i < index; i++) {
            System.out.println(rets[i]);
        }
    }

    public int getEnd(int[] ends, int i) {
        while (ends[i] != 0) {
            i = ends[i];
        }
        return i;
    }
}

class EData {
    char start;
    char end;
    int weight;

    public EData(char start, char end, int weight) {
        this.start = start;
        this.end = end;
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "EData{" +
                "<" + start +
                ", " + end +
                ">=" + weight +
                '}';
    }
}
